import React, { Component } from 'react';
import {
    StyleSheet,
    BackHandler,
    SafeAreaView,
    InteractionManager,
    DeviceEventEmitter
} from 'react-native';
import { msg, _ } from '../../../../common';
import XmList from '../../../../components/XmList';
import supply from '../../../../api/supply';
import LogisticsItem from '../source/components/LogisticsItem';



/**
 * @author kanglang
 * @description 发货历史列表
 * @createDate 2020/7/27
 */

export default class SendHistory extends Component {
    constructor(props) {
        super(props);
        this.state = {
            logisticsList: [],
            pageNo: 1,
            pageSize: 6,
            isLoading: true,
            showFoot: 1, // 控制foot， 0：隐藏footer  1：已加载完成,没有更多数据   2 ：显示加载中
            showFav: false
        }
    }

    componentDidMount() {
        this.listenser = DeviceEventEmitter.addListener('UPDATE_LOGISTICS', () => {
            InteractionManager.runAfterInteractions(() => {
                this.loadData();
            });
        });
        InteractionManager.runAfterInteractions(() => {
            this.loadData();
        });
        if (Platform.OS == 'android') {
            BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
        }

    }
    onBackAndroid = () => {
        this.xmHandleBack();
        return true
    }
    componentWillUnmount() {
        this.listenser.remove();
        if (Platform.OS == 'android') {
            BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
        }
    }
    // 下拉刷新
    loadData() {
        this.setState({
            isLoading: true,
            pageNo: 1,
            logisticsList: []
        }, () => {
            this.getLoadData(this.state.pageNo, this.state.pageSize, 'dropdown');
        });
    }

    // 上拉加载更多
    LoadMore() {
        this.setState({ showFoot: 2 })
        this.getLoadData(this.state.pageNo, this.state.pageSize, 'pullMore');
    }

    getLoadData = (pageNo, pageSize, type) => {
        const myMallParams = {
            pageNo,
            pageSize,
            state: 2
        }
        supply.pageSupplyIndex(myMallParams).then(res => {
            if (res.success) {
                let logisticsList = this.state.logisticsList;
                const newList = res.data.result;
                if (res.data.total == 0) {
                    this.setState({
                        isLoading: false,
                        showFoot: 0
                    });
                    return;
                };
                if (newList.length <= 0) {
                    this.setState({
                        isLoading: false,
                        showFoot: 1
                    });
                    return;
                };

                if (type == 'dropdown') {
                    logisticsList = newList.concat(logisticsList);
                    this.setState({ isLoading: false });
                } else if (type == 'pullMore') {
                    logisticsList = logisticsList.concat(newList);
                    this.setState({ isLoading: false });
                }
                this.setState({
                    pageNo: this.state.pageNo + 1,
                    logisticsList
                });
                if (res.data.result.length <= this.state.pageSize) {
                    this.setState({
                        isLoading: false,
                        showFoot: 1
                    });
                }
            } else {
                this.setState({
                    isLoading: false,
                    showFoot: 0
                });
                msg.emit('app:tip', { text: "获取失败" });
            }
        }).catch(err => {
            this.setState({
                isLoading: false,
                showFoot: 0
            });
        });
    }
    // 再发一单
    handleFunc = (item) => {
        msg.emit('router: refreshRoute', {
            routeName: 'ReleaseSource',
        })
        msg.emit('router: goToNext', {
            routeName: 'ReleaseSource',
            // data: item
            id: item.id
        });
    }
    // 货源详情
    goodsDetails(item) {
        msg.emit('router: goToNext', {
            routeName: 'LogisticsDetail',
            id: item.id
        });
    }

    // 收藏/取消收藏
    handleFavorite(item) {
        msg.emit('app:tip', { text: "取消成功" });
        this.setState({showFav: !this.state.showFav});
    }

    render() {
        const { logisticsList, isLoading, showFoot,showFav } = this.state;

        const renderItem = ({ item }) => {
            return <LogisticsItem
                logisticsObj={item}
                hendleTitle="再发一单"
                handleStyle={{ borderColor: '#BA914A' }}
                wordStyle={{ color: '#BA914A' }}
                handleFunc={() => this.handleFunc(item)}
                goodsDetails={() => this.goodsDetails(item)}
                isHistory={true}
                isfavorite={true}
                showFav={showFav}
                handleFavorite={() => this.handleFavorite(item)}
            />
        };
        return (
            <SafeAreaView style={styles.container}>
                <XmList
                    sources={logisticsList}
                    renderItem={renderItem}
                    isLoading={isLoading}
                    onDropdownRefresh={() => this.loadData()}
                    onPullUpMore={() => this.LoadMore()}
                    singleKey={item => item.id}
                    showFoot={showFoot}
                    isHistory={true}
                />
            </SafeAreaView>
        );
    }

    xmHandleBack = () => {
        msg.emit('router: back');

    }
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F5F5F5'
    }
});